package com.ruoyi.web.controller.consume;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consume.domain.StromaApplyTemporayCon;
import com.ruoyi.consume.service.IStromaApplyTemporayConService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基质临时领用Controller
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Api("基质临时领用")
@RestController
@RequestMapping("/consume/stromatemap")
public class StromaApplyTemporayConController extends BaseController
{
    @Autowired
    private IStromaApplyTemporayConService stromaApplyTemporayService;

    /**
     * 查询基质临时领用列表
     */
    @ApiOperation("查询基质临时领用列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:list')")
    @GetMapping("/list")
    public TableDataInfo list(StromaApplyTemporayCon stromaApplyTemporayCon)
    {
        startPage();
        List<StromaApplyTemporayCon> list = stromaApplyTemporayService.selectStromaApplyTemporayList(stromaApplyTemporayCon);
        return getDataTable(list);
    }

    /**
     * 导出基质临时领用列表
     */
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:export')")
    @Log(title = "基质临时领用", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StromaApplyTemporayCon stromaApplyTemporayCon)
    {
        List<StromaApplyTemporayCon> list = stromaApplyTemporayService.selectStromaApplyTemporayList(stromaApplyTemporayCon);
        ExcelUtil<StromaApplyTemporayCon> util = new ExcelUtil<StromaApplyTemporayCon>(StromaApplyTemporayCon.class);
        return util.exportExcel(list, "基质临时领用数据");
    }

    /**
     * 获取基质临时领用详细信息
     */
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:query')")
    @GetMapping(value = "/{stromaApplyTemporayId}")
    public AjaxResult getInfo(@PathVariable("stromaApplyTemporayId") Long stromaApplyTemporayId)
    {
        return AjaxResult.success(stromaApplyTemporayService.selectStromaApplyTemporayById(stromaApplyTemporayId));
    }

    /**
     * 新增基质临时领用
     */
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:add')")
    @Log(title = "基质临时领用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StromaApplyTemporayCon stromaApplyTemporayCon)
    {
        return toAjax(stromaApplyTemporayService.insertStromaApplyTemporay(stromaApplyTemporayCon));
    }

    /**
     * 修改基质临时领用
     */
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:edit')")
    @Log(title = "基质临时领用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StromaApplyTemporayCon stromaApplyTemporayCon)
    {
        return toAjax(stromaApplyTemporayService.updateStromaApplyTemporay(stromaApplyTemporayCon));
    }

    /**
     * 删除基质临时领用
     */
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:remove')")
    @Log(title = "基质临时领用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stromaApplyTemporayIds}")
    public AjaxResult remove(@PathVariable Long[] stromaApplyTemporayIds)
    {
        return toAjax(stromaApplyTemporayService.deleteStromaApplyTemporayByIds(stromaApplyTemporayIds));
    }

    /**
     * 更新库存量表
     */
    @ApiOperation("更新库存量表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:amount')")
    @Log(title = "基质临时领用", businessType = BusinessType.UPDATE)
    @PutMapping("/amount")
    public AjaxResult amount(@RequestBody StromaApplyTemporayCon stromaApplyTemporayCon)
    {
        int row = stromaApplyTemporayService.updateAmount(stromaApplyTemporayCon);
        if(row == 0){
            return error("库存不足");
        }
        else {
            return toAjax(row);
        }
    }

    /**
     * 更新领用状态
     */
    @ApiOperation("更新领用状态")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:consu')")
    @Log(title = "基质临时领用", businessType = BusinessType.UPDATE)
    @PutMapping("/consu/{stromaApplyTemporayId}")
    public AjaxResult consume(@PathVariable("stromaApplyTemporayId") Long stromaApplyTemporayId)
    {
        return toAjax(stromaApplyTemporayService.updateConsumeStatus(stromaApplyTemporayId));
    }

    /**
     * 退回操作
     */
    @ApiOperation("退回操作")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:stromatemap:back')")
    @Log(title = "基质临时领用", businessType = BusinessType.UPDATE)
    @PutMapping("/back/{stromaApplyTemporayId}")
    public AjaxResult back(@PathVariable("stromaApplyTemporayId") Long stromaApplyTemporayId)
    {
        return toAjax(stromaApplyTemporayService.updateApprovalStatus(stromaApplyTemporayId));
    }
}
